﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;

namespace InseSoft.Infraestructure
{
    [DebuggerNonUserCode]
    public abstract class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        public void OnPropertyChanged<T>(Expression<Func<T>> property)
        {
            var propertyName = GetPropertyName(property);

            if (propertyName != null)
                OnPropertyChanged(propertyName);
        }

        public static string GetPropertyName<T>(Expression<Func<T>> property)
        {
            var expression = (MemberExpression)property.Body;
            return expression.Member.Name;
        }
    }
}
